package modules.lobby.view
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.lobby.LobbyEvent;
	
	import protobuf.HSPKG_MAIL_COUNT_ACK;
	
	import utils.FloatHandler;

	/**
	 * @author lee
	 */
	public class PlayerInfoView extends ViewManager
	{
		public function PlayerInfoView(mainUI:MovieClip)
		{
			this.mainUI = mainUI;
			init();
		}
		
		override protected function init():void
		{
			addEventListeners();
			updateView();
			mainUI.mailMc.buttonMode = true;
		}
		
		
		//显示邮件界面
		private function onShowMailUI(e:Event):void
		{
			dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_UI_MAIL));
		}
		
		//显示余额界面
		private function onBlanceClick(e:Event):void
		{
			dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_UI_BALANCE));	
		}
		
		//显示头像UI
		private function onAvatarClick(e:Event):void
		{
			dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_UI_AVATAR));
		}
		
		override protected function updateView():void
		{
			mainUI.availableTxt.text = GlobalData.myCurrency +FloatHandler.TwoFormatToString(GlobalData.balance);
			mainUI.usernameTxt.text = GlobalData.username;
			mainUI.vppTxt.text = FloatHandler.TwoFormatToString(GlobalData._myVPP)+'/'+FloatHandler.TwoFormatToString(GlobalData.currentMaxVPP);
			
			if(GlobalData.myVPPLevel == 0)
			{
				mainUI.vppStarMc.visible = false;
			}	
			else
			{
				mainUI.vppStarMc.gotoAndStop(GlobalData.myVPPLevel);
			}
			mainUI.expMc.scaleX = (GlobalData._myVPP - GlobalData.currentMinVPP)/(GlobalData.currentMaxVPP - GlobalData.currentMinVPP);
		}
		
		private function onGameEvent(e:GameEvent):void
		{
			updateView();
		}
		
		private function addEventListeners():void
		{
			dispatcher.addEventListener(HSProto.HSID_NAME_NICKNAME_ACK.toString(), onGameEvent);
			dispatcher.addEventListener(HSProto.HSID_USER_INFO_NTF.toString(),onGameEvent);
			dispatcher.addEventListener(HSProto.HSID_MAIL_COUNT_ACK.toString(),onChangeMailCount);
			
			
			mainUI.mailMc.addEventListener(MouseEvent.CLICK, onShowMailUI);
			mainUI.balanceBtn.addEventListener(MouseEvent.CLICK, onBlanceClick);
			mainUI.avatarMc.addEventListener(MouseEvent.CLICK, onAvatarClick);
		}
		
		protected function onChangeMailCount(event:GameEvent):void
		{
			mainUI.mailMc.msgCount.text = event.data;
		}
		
		private function removeEventListeners():void
		{
			
		}
	}
}